//
//  ImageAddPreView.m
//  TestAPP
//
//  Created by yangyong on 14-6-3.
//  Copyright (c) 2014年 gainline. All rights reserved.
//

#import "ImageAddPreView.h"

#define bgColor BPColor(238,238,238)

#define D_Pending_Width     10

@implementation ImageAddPreView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor redColor];
        [self initResource];
    }
    return self;
}

- (void)initResource
{
    _imageassets = [NSMutableArray array];
    _imageEditViewArray = [NSMutableArray array];
    //设置背景
    self.backgroundColor = bgColor;
    //设置提示文字信息
    CGFloat LabY = 10.0f;
    CGFloat LabX = 10.0f;
    _makePromptLab = [[UILabel alloc] initWithFrame:CGRectMake(LabX, LabY, 0, 0)];
    _makePromptLab.textColor = [UIColor blackColor];
    [self addSubview:_makePromptLab];
    
    //设置开始制作按钮同时和按钮里面的数字
    CGFloat readyMakeBtnH = 29.0;
    CGFloat readyMakeBtnY = 5.0f;
    CGFloat readyMakeBtnW = 100.0;
    CGFloat readyMakeBtnX = self.width - 7.0 - readyMakeBtnW;
    _readyMakeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _readyMakeBtn.frame = CGRectMake(readyMakeBtnX, readyMakeBtnY, readyMakeBtnW, readyMakeBtnH);
    [_readyMakeBtn setTitle:@"开始制作" forState:UIControlStateNormal];
    
    // 让button上的字向左移动
//    [_readyMakeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 16)];
    
    [_readyMakeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    _readyMakeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [_readyMakeBtn setBackgroundColor:BPGlobaThemeColor];
    [_readyMakeBtn.layer setCornerRadius:15.0f];
    [_readyMakeBtn addTarget:self action:@selector(startPintuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _readyMakeBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_readyMakeBtn];
    
    // 开始制作 后面的选择照片的 张数
//    _inBtnNumber = [[UIButton alloc] init];
//    CGFloat inBtnX = CGRectGetMaxX(_readyMakeBtn.frame);
//    CGFloat inBtnW = readyMakeBtnH - 4.0;
//    _inBtnNumber.frame = CGRectMake(inBtnX - 2 - inBtnW, readyMakeBtnY + 2 , inBtnW, inBtnW);
//    [_inBtnNumber setTitleColor:BPGlobaThemeColor forState:UIControlStateNormal];
//    [_inBtnNumber setTitle:@"0" forState:UIControlStateNormal];
//    [_inBtnNumber setTitle:@"0" forState:UIControlStateHighlighted];
//    [_inBtnNumber setBackgroundColor:[UIColor whiteColor]];
//    [_inBtnNumber.layer setCornerRadius:13.0f];
//    [_inBtnNumber addTarget:self action:@selector(startPintuButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:_inBtnNumber];
    
    //设置选好照片后的浏览界面
    CGFloat pickerViewY = CGRectGetMaxY(_readyMakeBtn.frame) + 8.0;
    CGFloat pickerViewH = self.height - pickerViewY - 8.0f;
    _pickerImageShowScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pickerViewY, self.width, pickerViewH)];
    _pickerImageShowScrollView.backgroundColor = bgColor;
    _pickerImageShowScrollView.showsHorizontalScrollIndicator = NO;
    _pickerImageShowScrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:_pickerImageShowScrollView];
}

- (void)setUpPictureNumber:(NSInteger)number
{
    self.number = number;
//    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"集%ld神图召唤台历1本!",(long)number]];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共需%ld张,已传0张",(long)number]];
    NSRange contentRange = {6,[content length] - 9};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:contentRange];
    if ((long)number <= 9) {
        
        [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(7,1)];
    }
    else{
        [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(8,1)];
    }
   
    
    _makePromptLab.attributedText = content;
    UIFont *textfont = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    _makePromptLab.font = textfont;
    CGSize labelS = [_makePromptLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_makePromptLab.font,NSFontAttributeName, nil]];
    _makePromptLab.size = labelS;
}

- (void)setUpYunPromptLabel:(NSInteger)number
{
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"制作仅需%ld张即可",number]];
    _makePromptLab.attributedText = content;
    UIFont *textfont = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    _makePromptLab.font = textfont;
    CGSize labelS = [_makePromptLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_makePromptLab.font,NSFontAttributeName, nil]];
    _makePromptLab.size = labelS;
}

#pragma - mark 添加图片
- (void)addImageWith:(ALAsset *)asset
{
    NSLog(@"数组 = %@", _imageassets);
    
    CGFloat imageViewW = _pickerImageShowScrollView.height;
    if(_imageassets.count < self.picnum){
        [_imageassets addObject:asset];
        [_inBtnNumber setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_imageassets.count] forState:UIControlStateNormal];
        [_inBtnNumber setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_imageassets.count] forState:UIControlStateHighlighted];
        
//        UILabel *num = [[UILabel alloc] init];
//        num.textColor = BPGlobaThemeColor;
//        num.text = [NSString stringWithFormat:@"%lu",(unsigned long)_imageassets.count];

        
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共需%ld张,已传%lu张",self.number,(unsigned long)_imageassets.count]];
        NSRange contentRange = {6,[content length] - 9};
        [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:contentRange];
//        [content addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:contentRange];
        
        if (self.number <= 9) {
            [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(7,1)];
        }
        else if (self.number > 9 && self.number <= 99){
            if ((unsigned long)_imageassets.count <= 9) {
                
                [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(8,1)];
            }else{
                [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(8,2)];

            }
        }
        
        _makePromptLab.attributedText = content;
        UIFont *textfont = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
        _makePromptLab.font = textfont;
        CGSize labelS = [_makePromptLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_makePromptLab.font,NSFontAttributeName, nil]];
        _makePromptLab.size = labelS;
        
        
        
        _pickerImageShowScrollView.contentSize = CGSizeMake([_imageassets count] *imageViewW, _pickerImageShowScrollView.height);
        if ([_imageassets count]*(imageViewW+D_Pending_Width)+D_Pending_Width > _pickerImageShowScrollView.width) {
            [self.pickerImageShowScrollView setContentOffset:CGPointMake(self.pickerImageShowScrollView.contentSize.width - _pickerImageShowScrollView.frame.size.width, 0) animated:YES];
        }
        ImageEditView *editView = [[ImageEditView alloc] initWithFrame:CGRectMake([_imageassets count]*(imageViewW+D_Pending_Width)+D_Pending_Width,0,imageViewW,imageViewW)];
        
        [editView setImageAsset:asset index:[_imageassets count]-1];
        editView.deleteEdit = self;
        [_imageEditViewArray addObject:editView];
        [_pickerImageShowScrollView addSubview:editView];
        [self resetContentView];
        if ([_imageassets count]*(imageViewW+D_Pending_Width)+D_Pending_Width > _pickerImageShowScrollView.width) {
            [self.pickerImageShowScrollView setContentOffset:CGPointMake(self.pickerImageShowScrollView.contentSize.width - _pickerImageShowScrollView.width, 0) animated:YES];
        }
        
    }else{
        NSString *mes = [NSString stringWithFormat:@"制作仅需%d张即可",self.picnum];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

- (void)deletePintuAction:(ALAsset *)asset
{
    for (ImageEditView *tempEditView in _imageEditViewArray) {
        if ([tempEditView.asset.defaultRepresentation.url isEqual:asset.defaultRepresentation.url] ) {
            [self responseToDelete:tempEditView];
            break;
        }
    }
}

- (void)startPintuButtonAction:(id)sender
{
    if (_delegateSelectImage && [_delegateSelectImage respondsToSelector:@selector(startPintuAction:)]) {
        [_delegateSelectImage startPintuAction:self];
    }
}

#pragma mark ImageEditViewDelegate
- (void)responseToDelete:(id)sender
{
    ImageEditView *imageEditView  = (ImageEditView *)sender;
    [imageEditView removeFromSuperview];
    [_imageEditViewArray removeObject:imageEditView];
    [_imageassets removeObjectAtIndex:imageEditView.index];
    [self resetContentView];
}

- (void)resetContentView
{
    CGFloat imageViewW = _pickerImageShowScrollView.height;
    for (int i = 0; i < [_imageassets count]; i++) {
        ImageEditView *tempEditView = [_imageEditViewArray objectAtIndex:i];
        tempEditView.index = i;
        [UIView animateWithDuration:0.2
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             tempEditView.frame = CGRectMake(i*(imageViewW+D_Pending_Width)+D_Pending_Width,
                                                             0,
                                                             imageViewW,
                                                             imageViewW);
                         } completion:^(BOOL finished) {
                             
                         }];
        
    }
    _pickerImageShowScrollView.contentSize = CGSizeMake([_imageassets count]*(imageViewW+D_Pending_Width)+D_Pending_Width, _pickerImageShowScrollView.frame.size.height);
    
    [_inBtnNumber setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_imageassets.count] forState:UIControlStateNormal];
    [_inBtnNumber setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_imageassets.count] forState:UIControlStateHighlighted];
    
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共需%ld张,已传%lu张",self.number,(unsigned long)_imageassets.count]];
    NSRange contentRange = {6,[content length] - 9};
    [content addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0f] range:contentRange];
//    [content addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:contentRange];
//     [content addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7,2)];
    
    if (self.number <= 9) {
        [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(7,1)];
    }
    else if (self.number > 9 && self.number <= 99){
        if ((unsigned long)_imageassets.count <= 9) {
            
            [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(8,1)];
        }else{
            
            [content addAttribute:NSForegroundColorAttributeName value:BPGlobaThemeColor range:NSMakeRange(8,2)];
            
        }
    }
    
    _makePromptLab.attributedText = content;
    UIFont *textfont = [UIFont fontWithName:@"HelveticaNeue" size:14.0f];
    _makePromptLab.font = textfont;
    CGSize labelS = [_makePromptLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:_makePromptLab.font,NSFontAttributeName, nil]];
    _makePromptLab.size = labelS;
}

- (void)reMoveAllResource
{
    [self.imageassets removeAllObjects];
    [self.imageEditViewArray removeAllObjects];
    for (UIView *v in  self.pickerImageShowScrollView.subviews) {
        [v removeFromSuperview];
    }
    [self resetContentView];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
